package com.lwj.service.configuration;

import com.lwj.dto.configuration.*;
import com.lwj.exception.NoPermissionException;
import com.lwj.model.administration.Department;
import com.lwj.model.configuration.DepartmentRoleMenu;
import com.lwj.service.SpringUtil;
import com.lwj.service.administration.DepartmentService;
import com.lwj.utils.UuidUtil;
import com.lwj.vo.PageBean;
import com.lwj.vo.configuration.DepartmentRoleMenuVo;
import com.lwj.vo.configuration.PermissionCheckVo;
import com.lwj.vo.configuration.RoleVo;
import com.lwj.vo.configuration.UserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl  implements PermissionService {
    @Autowired
    private DepartmentRoleService departmentRoleService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private DepartmentRoleMenuService departmentRoleMenuService;
    @Autowired
    private DepartmentService departmentService;
    private static ThreadLocal<PermissionCheckVo> permissionMap = new ThreadLocal<>();

    @Override
    public PageBean<RoleVo> roleList(RoleListDto dto) {
        return departmentRoleService.roleList(dto);
    }

    @Override
    public String roleSave(DepartmentRoleDto dto) {
        return departmentRoleService.roleSave(dto);
    }

    @Override
    public PageBean<UserRoleVo> getUserByRole(UserRoleListDto dto) {
        return userRoleService.getUserByRole(dto);
    }

    @Override
    public  List<DepartmentRoleMenuVo> menuTreeByRoleId(String departmentRoleId) {
        return departmentRoleMenuService.menuTreeByRoleId(departmentRoleId);
    }

    @Override
    public void addUserToRole(UserRoleDto dto) {
         userRoleService.addUserToRole(dto);
    }

    @Override
    public void deleteUserRole(String departmentRoleId, String empId) {
        userRoleService.deleteUserRole(departmentRoleId,empId);
    }

    @Override
    public void savePermission(DepartmentRoleMenuDto dto) {
        departmentRoleMenuService.savePermission(dto);
    }

    @Override
    public PermissionCheckVo permissionCheck(String departmentId) {
        PermissionCheckVo permissionCheckVo = permissionMap.get();
        if(!"0".equals(permissionCheckVo.getCurrentLevel())&&departmentId.equals(permissionCheckVo.getDepartmentId())){
             return permissionCheckVo;
        }
        if(!"0".equals(permissionCheckVo.getNextLevel())){
            Department target = departmentService.selectByPrimaryKey(departmentId);
            Department role = departmentService.selectByPrimaryKey(permissionCheckVo.getDepartmentId());
            if(target.getPath().startsWith(role.getPath()) && !departmentId.equals(permissionCheckVo.getDepartmentId())){
                return permissionCheckVo;
            }
        }
        throw new NoPermissionException();
    }

    @Override
    public PermissionCheckVo checkForList(String departmentId) {
        PermissionCheckVo permissionCheckVo = permissionMap.get();
        Department target = departmentService.selectByPrimaryKey(departmentId);
        Department role = departmentService.selectByPrimaryKey(permissionCheckVo.getDepartmentId());
        if(!target.getPath().startsWith(role.getPath())){
            throw new NoPermissionException();
        }
        permissionCheckVo.setTarget(departmentId);
        permissionCheckVo.setTargetPath(target.getPath());
        return permissionCheckVo;
    }

    @Override
    public void initAdminPermission(String menuId) {
        List<String> list = departmentRoleService.selectAdminIds(false);
        List<DepartmentRoleMenu> saveList=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String roleId = list.get(i);
            DepartmentRoleMenu departmentRoleMenu=new DepartmentRoleMenu(UuidUtil.getUuid(),roleId,menuId,"1","1");
            departmentRoleMenu.initCreate(SpringUtil.getUserId());
            saveList.add(departmentRoleMenu);
        }
        departmentRoleMenuService.saveList(saveList);
    }

    @Override
    public PermissionCheckVo getPermissionCheckVo() {
        return permissionMap.get();
    }

    @Override
    public void initPermissionCheckVo(String code ) {
        PermissionCheckVo permissionCheckVo = selectPermission(code);
        permissionMap.set(permissionCheckVo);
    }

    @Override
    public void removePermissionCheckVo() {
        permissionMap.remove();
    }


    public PermissionCheckVo selectPermission(String code){
        String departmentRoleId = SpringUtil.getDepartmentRoleId();
        String empId = SpringUtil.getUserId();
        boolean check= !StringUtils.hasText(code)|| !StringUtils.hasText(departmentRoleId)|| !StringUtils.hasText(empId);
        if(check){
            throw new NoPermissionException();
        }
        PermissionCheckVo permissionCheckVo= departmentRoleService.selectPermission(code,departmentRoleId,empId);
        if(permissionCheckVo==null){
            throw new NoPermissionException();
        }
        permissionCheckVo.setPermissionCode(code);
        return permissionCheckVo;
    }

}
